/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Army;

import Army.ArmyEntity;
import Army.task.Task;
import Army.unit.Unit;
import GameObject.Sprite;
import MOM.Center;
import MOM.Message;
import MOM.Publisher;
import java.awt.Point;
import java.awt.Rectangle;

/**
 *
 * @author Huy
 */
public class BuildingAndUnit extends ArmyEntity implements Publisher {

	public static int BUILD_SPEED = 1;

	@Override
	public void post(int i, Message message) {
		Center.post(i, message);
	}

	public enum BuildingAndUnitState {

		IDLE, RUN, ATTACK, DIE
	};
	protected boolean completed = true;
	protected boolean isSelected = false;
	protected Point currentTarget;
	protected Task task;
	protected int healthPoint;
	protected int maxHealthPoint;
	protected double viewRange;
	protected double attackRange;
	protected int attackSpeed;
	protected double speed;
	protected BuildingAndUnitState state;
	protected int damgeScore;
	protected int armorScore;
	protected int buildCost;
	protected int food;

	public int getBuildCost() {
		return buildCost;
	}

	public int getFood() {
		return food;
	}

	public int getDamgeScore() {
		return damgeScore;
	}

	public int getArmorScore() {
		return armorScore;
	}

	public double getViewRange() {
		return viewRange;
	}

	public double getAttackRange() {
		return attackRange;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Task getTask() {
		return task;
	}

	public boolean getCompleted() {
		return completed;
	}

	public double getSpeed() {
		return speed;
	}

	public boolean isTaskCompleted() {
		return (task == null || task.isIsDone());
	}

	public static enum Type {

		Null,
		BuildingAndUnit,
		Unit,
		UnitArcher,
		UnitWorker,
		UnitSpear,
		UnitSword,
		Building,
		BuildingTower,
		BuildingCastle,
		BuildingBarrack,
		BuildingHouse,
		BuildingMine
	}
	protected Type type;

	public Type getType() {
		return type;

	}
	private int groupNumber;

	public int getGroupNumber() {
		return groupNumber;
	}

	public void setGroupNumber(int number) {
		groupNumber = number;
	}

	public void setTarget(Point pos) {
		currentTarget = pos;
	}

	public Point getCurrentTarget() {
		return currentTarget;
	}

	public BuildingAndUnit() {
	}

	public boolean isIsSelected() {
		return isSelected;
	}

	public void select() {
		this.isSelected = true;
	}

	public void disSelect() {
		this.isSelected = false;
	}

	public Rectangle getBound() {
		return new Rectangle(getPosition().x - size / 2, getPosition().y - size / 2, size, size);
	}

	public Sprite getHitedEffect(int x, int y) {
		return null;
	}

	public int getHealthPoint() {
		return healthPoint;
	}

	public int getMaxHealthPoint() {
		return maxHealthPoint;
	}

	public void incHealthPoint(int dHp) {
		healthPoint += dHp;
		if (healthPoint < 0) {
			healthPoint = 0;
		} else if (healthPoint > maxHealthPoint) {
			healthPoint = maxHealthPoint;
		}
	}

	public boolean isAlive() {
		return healthPoint > 0;
	}

	public boolean isBuilding() {
		return false;
	}

	public boolean isUnit() {
		return false;
	}

	public BuildingAndUnit create(int team, int x, int y) {
		return null;
	}

	@Override
	public void update() {
		super.update();
		if (state == BuildingAndUnitState.DIE) {
			if (sprite.isLastFrame()) {
				isDetroyed = true;
				post(team, new Message(this, "died"));
			}
			return;
		}
		if (healthPoint == 0) {
			//isDetroyed = true;
			die();
		} else if (task != null) {
			task.execute();
		}
	}

	public void build() {
		if (!completed) {
			healthPoint += BUILD_SPEED;
			if (healthPoint >= maxHealthPoint) {
				healthPoint = maxHealthPoint;
				post(team, new Message(this, "completed"));
				completed = true;
			}
		}

	}

	public void startBuild() {
		completed = false;
		healthPoint = 1;
	}

	public boolean isCompleted() {
		return completed;
	}

	public void die() {
		if (state == BuildingAndUnitState.DIE) {
			return;
		}
		state = BuildingAndUnitState.DIE;
	}

	public void run() {
		if (state == BuildingAndUnitState.RUN || state == BuildingAndUnitState.DIE) {
			return;
		}
		state = BuildingAndUnitState.RUN;
	}

	public void idle() {
		if (state == BuildingAndUnitState.IDLE || state == BuildingAndUnitState.DIE) {
			return;
		}
		state = Unit.BuildingAndUnitState.IDLE;

	}

	public void attack() {
		if (state == Unit.BuildingAndUnitState.ATTACK || state == Unit.BuildingAndUnitState.DIE) {
			return;
		}
		state = Unit.BuildingAndUnitState.ATTACK;

	}
}
